// 实现简单的promise
function p() {
  this.status = 'pending'
  this.msg = ''
  let that = this
  let process = arguments[0]
  process(function () {
    that.status = 'resolve'
    that.msg = arguments[0]
  }, function () {
    that.status = "reject"
    that.msg = arguments[0]
  })
}
p.prototype.then = function () {
  if (this.status === 'resolve') {
    arguments[0](this.msg)
  } else if (this.status === 'reject' && arguments[1]) {
    arguments[1](this.msg)
  }
}
let t = new p((resolve, reject) => { resolve(1) })
t.then((x) => { console.log(x) })

//实现promise.all
function promiseAll(promiseArr) {
  if (!Array.isArray(promiseArr)) {
    return "参数为数组"
  }
  return new Promise(function (resolve, reject) {
    let resolveValues = []
    let resolveCount = 0
    for (let i = 0; i < promiseArr.length; i++) {
      Promise.resolve(promiseArr[i]).then(function (res) {
        console.log("res", res)
        // 和promise.race的主要差别
        resolveCount++
        resolveValues[i] = res
        if (resolveCount === promiseArr.length) {
          resolve(resolveValues)
        }
      }, function (err) {
        reject(err)
      })
    }
  })
}
//实现promise.race
function promiseRace(promiseArr) {
  if (!Array.isArray(promiseArr)) {
    return "参数为数组"
  }
  return new Promise(function (resolve, reject) {
    for (let i = 0; i < promiseArr.length; i++) {
      Promise.resolve(promiseArr[i]).then((res) => {
        resolve(res) // 和promise.all的主要差别
      }, (err) => {
        reject(err)
      })
    }
  })
}